package com.oneonefive.educationsystem.handler;

import com.alibaba.fastjson2.JSON;
import com.oneonefive.educationsystem.domain.Login;
import com.oneonefive.educationsystem.dto.Result;
import com.oneonefive.educationsystem.utils.WebUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class logoutHandler implements LogoutHandler {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        System.out.println(authentication);
        Login user = (Login) authentication.getPrincipal();
        Integer username = user.getUser().getUserId();
        stringRedisTemplate.delete("login:" + username);
        Result result = Result.ok();
        String jsonString = JSON.toJSONString(result);
        WebUtils.renderString(response, jsonString);
    }
}
